package com.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.mapper.ForumArticleMapper;
import com.forum.mapper.StudentInfoMapper;
import com.forum.pojo.ArticleTipoffInfo;
import com.forum.pojo.ForumArticle;
import com.forum.pojo.StudentInfo;
import com.forum.pojo.UserTipoffInfo;
import com.forum.pojo.vo.ArticleTipOffVo;
import com.forum.service.ArticleTipoffInfoService;
import com.forum.mapper.ArticleTipoffInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86152
* @description 针对表【article_tipoff_info】的数据库操作Service实现
* @createDate 2023-02-21 16:21:21
*/
@Service
public class ArticleTipoffInfoServiceImpl extends ServiceImpl<ArticleTipoffInfoMapper, ArticleTipoffInfo>
    implements ArticleTipoffInfoService{
    @Autowired
    ArticleTipoffInfoMapper articleTipoffInfoMapper;
    @Autowired
    StudentInfoMapper studentInfoMapper;
    @Autowired
    ForumArticleMapper forumarticleMapper;
    @Override
    public void saveTipOff(Long userId,String articleId, String description) {
        QueryWrapper<ForumArticle> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("article_id",articleId);
        Long informedUserId = forumarticleMapper.selectOne(queryWrapper).getUserId();

        ArticleTipoffInfo articleTipoffInfo=new ArticleTipoffInfo();
        articleTipoffInfo.setArticleId(articleId);
        articleTipoffInfo.setUserId(userId);
        articleTipoffInfo.setInformedUserId(informedUserId);
        articleTipoffInfo.setDescription(description);

        articleTipoffInfoMapper.insert(articleTipoffInfo);
        StudentInfo studentInfo = studentInfoMapper.selectById(informedUserId);
        studentInfo.setIllegalcount(studentInfo.getIllegalcount()+1);

        studentInfoMapper.updateById(studentInfo);
    }

    @Override
    public Page<ArticleTipoffInfo> getTipOff(Integer searchType, Integer pageNo, Integer pageSize) {
        List<ArticleTipoffInfo> articleTipoffInfos = articleTipoffInfoMapper.selectList(null);
        List<ArticleTipOffVo> result=new ArrayList<>();
        for (ArticleTipoffInfo articleTipoff:articleTipoffInfos) {
            ArticleTipOffVo articleTipOffVo=new ArticleTipOffVo();
            QueryWrapper<ForumArticle> queryWrapper=new QueryWrapper<>();
            QueryWrapper<StudentInfo> wrapper1=new QueryWrapper<>();

            //获得举报人名字
            wrapper1.eq("studentId",articleTipoff.getUserId());

            queryWrapper.eq("p_board_id",searchType);
            queryWrapper.eq("article_id", articleTipoff.getArticleId());
            ForumArticle article1 = forumarticleMapper.selectOne(queryWrapper);

            articleTipOffVo.setTioffId(articleTipoff.getTipoffId());
            if(article1!=null){
                articleTipOffVo.setPboardName(article1.getpBoardName());
                articleTipOffVo.setArticleId(article1.getArticleId());
                articleTipOffVo.setTitle(article1.getTitle());
                articleTipOffVo.setTag(article1.getTag());
                articleTipOffVo.setNickName(article1.getNickName());
                articleTipOffVo.setUserId(article1.getUserId());
            }
            articleTipOffVo.setDesc(articleTipoff.getDescription());
            articleTipOffVo.setUserNickName(studentInfoMapper.selectOne(wrapper1).getNickname());
            result.add(articleTipOffVo);
        }
            return getPages(pageNo,pageSize,result);
    }
    public static Page getPages(Integer currentPage, Integer pageSize, List list){

        Page page = new Page();

        int size = list.size();
        if(size==0){
            return null;
        }
        if(pageSize > size){
            pageSize = size;
        }

        //求出最大页数，防止currentPage越界
        int maxPage = size % pageSize == 0 ? (size / pageSize) : (size / pageSize) + 1;

        if(currentPage > maxPage){
            currentPage = maxPage;
        }

        //当前页第一条数据下标
        int curIds = currentPage > 1 ? (currentPage -1) * pageSize : 0;

        List pageList = new ArrayList<>();

        //将当前页的数据放进pageList
        for (int i = 0; i < pageSize && curIds + i < size; i++) {
            pageList.add(list.get(curIds + i));
        }

        page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);

        return page;

    }
}




